package auctionhaus


import org.junit.*
import grails.test.mixin.*

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(Listing)
class ListingTests {

	Listing listing;
	@Before
	void setUp() {

		listing = new Listing();
	}

	@After
	void tearDown() {
		// Tear down logic here
	}
	//L-1: Listings have the following required fields: name, end date/time, and starting bid price (unit test)
	void testFieldsExist(){

		assertFalse(listing.validate(['name']));
		assertFalse(listing.validate(['endDateTime']));
//		assertFalse(listing.validate(['startingPrice']));

		listing. name = "2010 Ford Fusion"
		listing. endDateTime = new Date()+1;
		listing. startingPrice = 14000.75

		assertTrue(listing.validate(['name']));
		assertTrue(listing.validate(['endDateTime']));
		assertTrue(listing.validate(['startingPrice']));
	}
	//L-2: Listings have the following optional fields: description (unit test)
	void testDescriptionOptional(){
		assertNull(listing.description)
		assertTrue(listing.validate(['description']));
		listing.description = "This car is amazing and I love it... It goes fast."
		assertTrue(listing.validate(['description']));
	}
	//L-3: Listings are required to have a seller (Customer) (unit test)
	void testSellerRequired(){
		listing.name = "2010 Ford Fusion"
		listing.endDateTime = new Date();
		listing.startingPrice = 14000.75
		listing.description = "This car is amazing and I love it... It goes fast."

		assertNull(listing.seller)
		assertFalse(listing.validate(['seller']));
		listing.seller = new Customer()
		assertTrue(listing.validate(['seller']));
	}
	//L-4: Listing descriptions must be less than 256 characters (unit test)
	void testDescriptionLengthShort(){
		int length = 254
		String shortString = new String(new char[length] );
		assertEquals(length,shortString.length())
		
		listing.description = shortString
		assertTrue(listing.validate(['description']));
	}
	//L-4: Listing descriptions must be less than 256 characters (unit test)
	void testDescriptionLengthExact(){
		int length = 255
		String exactString = new String(new char[length] );
		assertEquals(length,exactString.length())
		
		listing.description = exactString
		assertTrue(listing.validate(['description']));
	}

	//L-4: Listing descriptions must be less than 256 characters (unit test)
	void testDescriptionLengthLong(){
		int length = 256
		String longString = new String(new char[length] );
		assertEquals(length,longString.length())
		
		listing.description = longString
		assertFalse(listing.validate(['description']));
	}
	
	//L-4: Listing descriptions must be less than 256 characters (unit test)
	void testDescriptionLengthZero(){
		int length = 0
		String longString = new String(new char[length] );
		assertEquals(length,longString.length())
		
		listing.description = longString
		assertTrue(listing.validate(['description']));
	}

	//L-5: Listing end date/time must be in the future (unit test)
	void testEndDateTimeFuture(){
		listing.endDateTime = (new Date())+1
		assertTrue(listing.validate(['endDateTime']))
	}
	//L-5: Listing end date/time must be in the future (unit test)
	void testEndDateTimePast(){
		listing.endDateTime = (new Date())-1
		assertFalse(listing.validate(['endDateTime']));
	}
	//L-6: Listing name must be less than 64 characters (unit test)
	void testNameLengthShort(){
		int length = 62
		String shortString = new String(new char[length] );
		assertEquals(length,shortString.length())
		
		listing.name = shortString
		assertTrue(listing.validate(['name']));
	}
	//L-6: Listing name must be less than 64 characters (unit test)
	void testNameLengthExact(){
		int length = 63
		String exactString = new String(new char[length] );
		assertEquals(length,exactString.length())
		
		listing.name = exactString
		assertTrue(listing.validate(['name']));
	}

	//L-6: Listing name must be less than 64 characters (unit test)
	void testNameLengthLong(){
		int length = 64
		String longString = new String(new char[length] );
		assertEquals(length,longString.length())

		listing.name = longString
		assertFalse(listing.validate(['name']));
	}
	
	//L-6: Listing name must be less than 64 characters (unit test)
	void testNameLengthZero(){
		int length = 0
		String longString = new String(new char[length] );
		assertEquals(length,longString.length())

		listing.name = longString
		assertFalse(listing.validate(['name']));
	}
	
	//L-7: Listing has a nullable field for the winner (Customer) (unit test)
	void testWinnerOptional(){
		assertNull(listing.winner)
		assertTrue(listing.validate(['winner']));
		
		listing.winner = new Customer();
		assertTrue(listing.validate(['winner']));
	}

}
